package com.neuedu.oasystem.view;

import java.util.List;
import java.util.Scanner;

import com.neuedu.oasystem.entity.Role;
import com.neuedu.oasystem.service.RoleService;
import com.neuedu.oasystem.service.impl.RoleServiceImpl;

public class RoleSystem {
	private Scanner scanner = null;
	Role role = null;
	private RoleService roleService;

	public void init() {
		System.out.println("========================================");
		System.out.println("           欢迎使用角色管理系统                                 ");
		System.out.println("========================================");
		System.out.println("1.角色管理");
		System.out.println("2.退出系统");
		System.out.println("========================================");
	}

	public void work() {
		scanner = new Scanner(System.in);
		roleService = new RoleServiceImpl();
		boolean running = true;
		while (running) {
			init();
			System.out.print("请选择：");
			String choose = scanner.next();
			switch (choose) {
			case "1":
				// 调用用管理模块
				System.out.println("调用用管理模块");
				userManager();
				break;
			case "2":
				System.out.println("您选择了退出系统！");
				running = false;
				break;
			default:
				System.out.println("您的输入有误！");
				break;
			}
		}
	}

	private void userManager() {

		boolean running = true;
		while (running) {
			System.out.println("========================================");
			System.out.println("            您选了角色管理模块                                   ");
			System.out.println("========================================");
			System.out.println("1.新增角色");
			System.out.println("2.修改角色");
			System.out.println("3.角色列表");
			System.out.println("4.删除角色");
			System.out.println("5.返回上一级");
			System.out.println("========================================");
			System.out.println("请选择：");
			String choose = scanner.next();
			switch (choose) {
			case "1":
				System.out.println("新增角色 ");
				roleAdd();
				break;
			case "2":
				System.out.println("修改角色 ");
				roleEdit();
				break;
			case "3":
				System.out.println("角色列表");
				roleList();
				break;
			case "4":
				System.out.println("删除角色 ");
				roleDelete();
				break;
			case "5":
				System.out.println("返回上一级");
				running = false;
				break;
			default:
				System.out.println("您的输入有误！");
				break;
			}

		}

	}

	private void roleAdd() {
		roleService = new RoleServiceImpl();
		System.out.println("请输入角色ID:           ");
		String roleIdString;
		do {
			try {

				roleIdString = scanner.next();
				if (Integer.valueOf(roleIdString) instanceof Integer) {
					break;
				} else {
					roleIdString = scanner.next();
				}
			} catch (Exception e) {
				System.out.println("您的输入有误，请重新输入：");
			}
		} while (true);
		int roleId = Integer.parseInt(roleIdString);

		System.out.println("请添加角色名称");
		String roleName = scanner.next();
		System.out.println("请添加角色描述");
		String roleDescription = scanner.next();
		role = new Role(roleId, roleName, roleDescription);
		
		boolean b = roleService.add(role);
		if (b) {
			System.out.println("新增成功！");
		}else {
			System.out.println("新增失败！");
		}
	}

	private void roleEdit() {
		roleList();
		roleService = new RoleServiceImpl();
		System.out.println("请输入修改角色的Id");
		int roleId = scanner.nextInt();
		System.out.println("请输入新的角色名称:        ");
		String roleName = scanner.next();
		System.out.println("请输入新的角色描述:    ");
		String roleDescription = scanner.next();
		role = new Role(roleId, roleName, roleDescription);
		
		boolean b = roleService.edit(role);
		if (b) {
			System.out.println("修改成功！");
		}else {
			System.out.println("修改失败！");
		}
	}

	private void roleList() {
		roleService = new RoleServiceImpl();
		List<Role> list = roleService.findAll();
		System.out.println("角色编号\t角色名称\t角色描述");
		for (Role role : list) {
			System.out.println(role.getRoleId() + "\t" + role.getRoleName() + "\t" + role.getRoleDescription());
		}
	}

	private void roleDelete() {
		roleService = new RoleServiceImpl();
		roleList();
		System.out.println("请输入您要删除的角色编号");
		int roleId = scanner.nextInt();
		try {
			roleService.deleteById(roleId);
		} catch (Exception e) {
			System.out.println("请输入正确的角色编号");
		}
		System.out.println("删除成功！");
		roleList();
	}
}
